package com.company.card_service.config.loginInterceptConfig;


import com.company.card_service.mapper.UserInfoMapper;
import com.company.card_service.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class UserLoginInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserInfoMapper userInfoMapper;

//    public UserLoginInterceptor(RedisTemplate redisTemplate) {
//        this.redisTemplate = redisTemplate;
//    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        return this.initUserLoginVo(request);
        return true;
    }

    private boolean initUserLoginVo(HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println(token);
        //String userId = request.getHeader("userId");
        if (StringUtils.isEmpty(token)) {
            log.info("龟儿登录了才可以进去");
            return false;
        } else {
            Long userId = JwtHelper.getUserId(token);
            if (StringUtils.isEmpty(userId)) {
                log.info("id为空，数据错误");
            } else {
                UserInfo userInfo = userInfoMapper.selectById(userId);
                AuthContextHolder.setUserId(Long.valueOf(userInfo.getId()));
            }

        }
        return true;
    }

}
